//
//  IntegralVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "IntegralVC.h"
#import "IntegralView.h"
#import "LotteryTypeView.h"
#import "MyVM.h"
#import "IntegralCell.h"
#import "LotteryTypeModel.h"
@interface IntegralVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic ,strong) LotteryTypeView *lotteryTypeView;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) NSMutableArray *totalArr;

@property (nonatomic ,strong) QXLable        *labIntegral;

@end

@implementation IntegralVC{
    NSInteger   selectType;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的积分";
    [self getData];
    [self.navigationController.navigationBar setBackgroundImage:[TGTools createImageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
}

- (void)getData{
    WS(weakSelf);
    LotteryTypeModel *lModel=[BaseVM manage].lotteryArr[selectType];
    [[MyVM manage] getBetWithPId:lModel.Id andSuccess:^(id returnValue) {
        weakSelf.dataArr=returnValue;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    [self.navigationController.navigationBar setBackgroundImage:[TGTools createImageWithColor:NAVCOLOR] forBarMetrics:UIBarMetricsDefault];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IntegralCell *cell=[tableView dequeueReusableCellWithIdentifier:@"IntegralCell" forIndexPath:indexPath];
    cell.model=self.dataArr[indexPath.row];
    return cell;
}

#pragma mark -- 懒加载
- (LotteryTypeView *)lotteryTypeView{
    if (!_lotteryTypeView) {
        WS(weakSelf);
        _lotteryTypeView=[[LotteryTypeView alloc]init];
        _lotteryTypeView.chaneBlock = ^(NSInteger row) {
            selectType=row;
            [weakSelf getData];
        };
        [self.view addSubview:_lotteryTypeView];
        [_lotteryTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(weakSelf.headView.mas_bottom);
            make.height.mas_equalTo(@40);
        }];
    }
    return _lotteryTypeView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight=70;
        [_tableView registerClass:[IntegralCell class] forCellReuseIdentifier:@"IntegralCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.view);
            make.top.equalTo(weakSelf.lotteryTypeView.mas_bottom)
            .with.offset(5);
        }];
    }
    return _tableView;
}


- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (UIView *)headView{
    if (!_headView) {
        _headView=[[UIView alloc]init];
        [self.view addSubview:_headView];
        [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.view);
            make.height.mas_equalTo(@(real_size(241)));
        }];
        _headView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Integral_bg"]];
        UIImageView *ballImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Integral_ball"]];
        [_headView addSubview:ballImage];
        [ballImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_headView);
            make.size.mas_equalTo(ballImage.image.size);
        }];
        
        _labIntegral =[[QXLable alloc]initWithBFount:15];
        _labIntegral.textAlignment=NSTextAlignmentCenter;
        _labIntegral.textColor=[UIColor whiteColor];
        _labIntegral.text=[UserInfoModel share].Integral;
        [_headView addSubview:_labIntegral];
        [_labIntegral mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(ballImage);
        }];
        
        QXLable *labTotal=[[QXLable alloc]initCenterTextWithText:@"总积分"];
        labTotal.textColor=[UIColor whiteColor];
        [_headView addSubview:labTotal];
        [labTotal mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(ballImage);
            make.top.equalTo(ballImage.mas_bottom).with.offset(10);
        }];
    }
    return _headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
